package com.bootdo.wx.service.impl;

import com.bootdo.wx.dao.*;
import com.bootdo.wx.domain.*;
import com.bootdo.wx.service.GoodsTypeService;
import com.bootdo.common.config.Constant;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.common.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.bootdo.common.utils.ShiroUtils.getUserId;


@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {
	@Autowired
	private GoodsTypeDao goodsTypeDao;
	@Autowired
	private GoodsTypeBrandDao goodsTypeBrandDao;
	@Autowired
	private GoodsTypeParamgDao goodsTypeParamgDao;
	@Autowired
	private GoodsTypeParamsDao goodsTypeParamsDao;
	@Autowired
	private GoodsTypePropsDao goodsTypePropsDao;
	@Autowired
	private GoodsTypePropsValuesDao goodsTypePropsValuesDao;
	@Autowired
	private GoodsTypeSpecDao goodsTypeSpecDao;
	@Autowired
	private GoodsTypeSpecValuesDao goodsTypeSpecValuesDao;
	
	@Override
	public GoodsTypeDO get(String id){
		return goodsTypeDao.get(id);
	}
	
	@Override
	public List<GoodsTypeDO> list(Map<String, Object> map){
		return goodsTypeDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return goodsTypeDao.count(map);
	}
	
	@Override
	public int save(GoodsTypeDO goodsType){
		return goodsTypeDao.save(goodsType);
	}
	
	@Override
	public int update(GoodsTypeDO goodsType){
		return goodsTypeDao.update(goodsType);
	}
	
	@Override
	public int remove(String id){
		return goodsTypeDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return goodsTypeDao.batchRemove(ids);
	}

	@Override
	public List<GoodsTypeDO> queryAll() {
		return goodsTypeDao.queryAll();
	}
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int add(GoodsTypeDO goodsType, String[] brand, String[] specId, String[] specValIds, String[] specValText,
			String[] specValUrl, String[] group_name, String[] group_params) {
		goodsType.setId(UuidUtil.get32UUID());
		goodsType.setDelFlag(Constant.DEFAULT_DEL_FLAG);
		Date date =new Date();
		goodsType.setGmtCreate(date);
		goodsType.setUserIdCreate(getUserId());
		goodsTypeDao.save(goodsType);
		if (brand != null ) {
			for (int i = 0; i < brand.length; i++) {
				GoodsTypeBrandDO typeBrand = new GoodsTypeBrandDO();
				typeBrand.setTypeid(goodsType.getId());
				typeBrand.setBrandid(brand[i]);
				typeBrand.setId(UuidUtil.get32UUID());
				typeBrand.setDelFlag(Constant.DEFAULT_DEL_FLAG);
				typeBrand.setGmtCreate(date);
				typeBrand.setLocation(i);
				typeBrand.setUserIdCreate(getUserId());
				goodsTypeBrandDao.save(typeBrand);
			}
		}

		if (specId != null ) {
			for (int i = 0; i < specId.length; i++) {
				GoodsTypeSpecDO typeSpec = new GoodsTypeSpecDO();
				typeSpec.setId(UuidUtil.get32UUID());
				typeSpec.setTypeId(goodsType.getId());
				typeSpec.setSpecId(specId[i]);
				typeSpec.setLocation(i);
				typeSpec.setDelFlag(Constant.DEFAULT_DEL_FLAG);
				typeSpec.setGmtCreate(date);
				typeSpec.setUserIdCreate(getUserId());
				goodsTypeSpecDao.save(typeSpec);
				String[] specValIdsTemp = new String[specId.length];
				String[] specValTextTemp = new String[specId.length];
				if (specId.length==1){
					//保存规格值
					specValIdsTemp = specValIds;
					specValTextTemp = specValText;
				}
				else {
					//保存规格值
					specValIdsTemp = StringUtils.split(specValIds[i], ",");
					specValTextTemp = StringUtils.split(specValText[i], ",");
				}

				for (int j = 0; j < specValIdsTemp.length; j++) {
					if (!StringUtils.isBlank(specValIdsTemp[j])) {
						GoodsTypeSpecValuesDO typeSpecValues = new GoodsTypeSpecValuesDO();
						typeSpecValues.setId(UuidUtil.get32UUID());
						typeSpecValues.setSpecId(specId[i]);
						typeSpecValues.setSpecValueId(specValIdsTemp[j]);
						typeSpecValues.setTypeId(goodsType.getId());
						typeSpecValues.setTypeSpecId(typeSpec.getId());
						typeSpecValues.setSpecValText(specValTextTemp[j]);
						typeSpecValues.setLocation(j);
						typeSpecValues.setDelFlag(Constant.DEFAULT_DEL_FLAG);
						typeSpecValues.setGmtCreate(date);
						typeSpecValues.setUserIdCreate(getUserId());
						goodsTypeSpecValuesDao.save(typeSpecValues);
					}
				}
			}
		}
		if (group_name != null ) {
			for (int i = 0; i < group_name.length; i++) {
				GoodsTypeParamgDO paramg = new GoodsTypeParamgDO();
				paramg.setId(UuidUtil.get32UUID());
				paramg.setTypeId(goodsType.getId());
				paramg.setName(group_name[i]);
				paramg.setLocation(i);
				paramg.setGmtCreate(date);
				paramg.setUserIdCreate(getUserId());
				paramg.setDelFlag(Constant.DEFAULT_DEL_FLAG);
				goodsTypeParamgDao.save(paramg);
				String[] params = new String[group_name.length];
				if (group_name.length==1){
					params = group_params;
				}else {
					params = StringUtils.split(group_params[i], ",");
				}
				for (int j = 0; j < params.length; j++) {
					if (!StringUtils.isBlank(params[j])){
						GoodsTypeParamsDO typeParams = new GoodsTypeParamsDO();
						typeParams.setId(UuidUtil.get32UUID());
						typeParams.setGroupId(paramg.getId());
						typeParams.setName(params[j]);
						typeParams.setLocation(j);
						typeParams.setTypeId(goodsType.getId());
						typeParams.setGmtCreate(date);
						typeParams.setUserIdCreate(getUserId());
						typeParams.setDelFlag(Constant.DEFAULT_DEL_FLAG);
						goodsTypeParamsDao.save(typeParams);
					}
				}
			}
		}
		return 1;
	}
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int edit(GoodsTypeDO goodsType, String[] brand, String[] specId, String[] specValIds, String[] specValText,
			String[] specValUrl, String[] group_name, String[] group_params) {
		Date date = new Date();
		goodsType.setGmtModified(date);
		goodsTypeDao.update(goodsType);

		goodsTypeBrandDao.removeByTypeId(goodsType.getId());
		if (brand != null ) {
			for (int i = 0; i < brand.length; i++) {
				GoodsTypeBrandDO typeBrand = new GoodsTypeBrandDO();
				typeBrand.setTypeid(goodsType.getId());
				typeBrand.setBrandid(brand[i]);
				typeBrand.setId(UuidUtil.get32UUID());
				typeBrand.setDelFlag(Constant.DEFAULT_DEL_FLAG);
				typeBrand.setGmtCreate(date);
				typeBrand.setLocation(i);
				typeBrand.setUserIdCreate(getUserId());
				goodsTypeBrandDao.save(typeBrand);
			}
		}

		goodsTypeSpecDao.removeByTypeId(goodsType.getId());
		if (specId != null ) {
			goodsTypeSpecValuesDao.removeByTypeId(goodsType.getId());
			for (int i = 0; i < specId.length; i++) {
				GoodsTypeSpecDO typeSpec = new GoodsTypeSpecDO();
				typeSpec.setId(UuidUtil.get32UUID());
				typeSpec.setTypeId(goodsType.getId());
				typeSpec.setSpecId(specId[i]);
				typeSpec.setLocation(i);
				typeSpec.setDelFlag(Constant.DEFAULT_DEL_FLAG);
				typeSpec.setGmtCreate(date);
				typeSpec.setUserIdCreate(getUserId());
				goodsTypeSpecDao.save(typeSpec);
				//保存规格值
				String[] specValIdsTemp = new String[specId.length];
				String[] specValTextTemp = new String[specId.length];
				if (specId.length==1){
					//保存规格值
					specValIdsTemp = specValIds;
					specValTextTemp = specValText;
				}
				else {
					//保存规格值
					specValIdsTemp = StringUtils.split(specValIds[i], ",");
					specValTextTemp = StringUtils.split(specValText[i], ",");
				}
				for (int j = 0; j < specValIdsTemp.length; j++) {
					if (!StringUtils.isBlank(specValIdsTemp[j])) {
						GoodsTypeSpecValuesDO typeSpecValues = new GoodsTypeSpecValuesDO();

						typeSpecValues.setSpecId(specId[i]);
						typeSpecValues.setSpecValueId(specValIdsTemp[j]);
						typeSpecValues.setTypeId(goodsType.getId());
						typeSpecValues.setTypeSpecId(typeSpec.getId());
						typeSpecValues.setSpecValText(specValTextTemp[j]);
						typeSpecValues.setLocation(j);
						typeSpecValues.setDelFlag(Constant.DEFAULT_DEL_FLAG);
						typeSpecValues.setGmtCreate(date);
						typeSpecValues.setUserIdCreate(getUserId());
							typeSpecValues.setId(UuidUtil.get32UUID());
							int save = goodsTypeSpecValuesDao.save(typeSpecValues);
							System.out.println(save);
						/*}else {
							typeSpecValues.setId(specId[i]);
							int update = goodsTypeSpecValuesDao.update(typeSpecValues);
							System.out.println(update);
						}*/

					}

				}
			}
		}
		goodsTypeParamsDao.removeByTypeId(goodsType.getId());
		goodsTypeParamgDao.removeByTypeId(goodsType.getId());
		if (group_name != null) {
			for (int i = 0; i < group_name.length; i++) {
				GoodsTypeParamgDO paramg = new GoodsTypeParamgDO();
				paramg.setId(UuidUtil.get32UUID());
				paramg.setTypeId(goodsType.getId());
				paramg.setName(group_name[i]);
				paramg.setLocation(i);
				paramg.setGmtCreate(date);
				paramg.setUserIdCreate(getUserId());
				paramg.setDelFlag(Constant.DEFAULT_DEL_FLAG);
				goodsTypeParamgDao.save(paramg);


				String[] params = new String[group_name.length];
				if (group_name.length==1){
					params = group_params;
				}else {
					params = StringUtils.split(group_params[i], ",");
				}
				for (int j = 0; j < params.length; j++) {
					if (!StringUtils.isBlank(params[j])){
						GoodsTypeParamsDO typeParams = new GoodsTypeParamsDO();
						typeParams.setId(UuidUtil.get32UUID());
						typeParams.setGroupId(paramg.getId());
						typeParams.setName(params[j]);
						typeParams.setLocation(j);
						typeParams.setTypeId(goodsType.getId());
						typeParams.setGmtCreate(date);
						typeParams.setUserIdCreate(getUserId());
						typeParams.setDelFlag(Constant.DEFAULT_DEL_FLAG);
						goodsTypeParamsDao.save(typeParams);
					}
				}
			}
		}
		return 1;
		
	}
	
}
